package club.banyuan.reserve.service;

import club.banyuan.reserve.common.CommonResult;
import club.banyuan.reserve.dao.AuthDao;
import club.banyuan.reserve.mapper.UserMapper;
import club.banyuan.reserve.model.Role;
import club.banyuan.reserve.model.User;
import club.banyuan.reserve.model.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author HanChao
 * 描述信息：
 */
@Service
public class AdminServiceImpl implements AdminService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AuthDao authDao;

    @Override
    public User adminUserLogin (String username, String password) {

        UserExample userExample = new UserExample();
        userExample.createCriteria()
                .andUsernameEqualTo(username)
                .andPasswordEqualTo(password);
        List<User> userList = userMapper.selectByExample(userExample);

        if (userList.size() == 0) {

            return null;
        }

        List<Role> role = authDao.findRoleByUserId(userList.get(0).getId());

        if (role.contains("ADMIN") == false) {

            return null;
        }

        User user = userList.get(0);

        return user;
    }

    @Override
    public void logout(HttpSession session, String sessionKey) {

        session.removeAttribute(sessionKey);
    }
}
